<div class="layui-card">
    <div class="layui-card-header">
        <h2 class="header-title">基本任务</h2>
        <span class="layui-breadcrumb pull-right">
          <a href="#!home_console">首页</a>
          <a><cite>基本任务</cite></a>
        </span>
    </div>
    <div class="layui-card-body">
        <div class="layui-form toolbar">
            搜索：
            <input id="task-search-value" class="layui-input search-input" type="text" placeholder="输入关键字"/>
            <button id="task-btn-search" class="layui-btn icon-btn"><i class="layui-icon">&#xe615;</i>搜索</button>
        </div>

        <table class="layui-table" id="task-table" lay-filter="task-table"></table>
    </div>
</div>

<!-- 表头操作列 -->
<script type="text/html" id="task-table-bar">
    <div class="layui-inline" lay-event="add" lay-tips="新增"><i class="layui-icon layui-icon-add-1"></i></div>
    <div class="layui-inline" lay-event="edit" lay-tips="修改"><i class="layui-icon layui-icon-edit"></i></div>
    <div class="layui-inline" lay-event="del" lay-tips="删除"><i class="layui-icon layui-icon-delete"></i></div>
</script>

<script>
    layui.use(['form', 'table', 'util', 'admin', 'element', 'jquery'], function () {
        const $ = layui.jquery;
        const table = layui.table;
        const layer = layui.layer;
        const admin = layui.admin;

        // 渲染表格
        table.render({
            elem: '#task-table',
            url: basePath + 'batch/task/list',
            toolbar: '#task-table-bar',
            defaultToolbar: ['filter'],
            page: true,
            cols: [
                [
                    {type: 'radio', fixed: 'left'},
                    {field: 'code', sort: true, title: '任务编码'},
                    {field: 'name', sort: true, title: '任务名称'},
                    {field: 'createDate', sort: true, title: '创建时间'},
                    {field: 'updateDate', sort: true, title: '修改时间'},
                ]
            ]
        });


        // 工具条点击事件
        table.on('toolbar(task-table)', function (obj) {
            const checkStatus = table.checkStatus(obj.config.id); //获取选中行状态
            const data = checkStatus.data;  //获取选中行数据

            switch (obj.event) {
                case 'add':
                    addOrEdit();
                    break;
                case 'edit':
                    if (data.length === 0) {
                        layer.msg('请选择一行');
                        return;
                    }
                    addOrEdit(data[0]);
                    break;
                case 'del':
                    if (data.length === 0) {
                        layer.msg('请选择一行');
                        return;
                    }
                    del(data[0].id);
                    break;
            }
        });

        // 显示表单弹窗
        const addOrEdit = function (data) {
            const title = data ? '修改基本任务' : '添加基本任务';
            admin.putTempData('t_task', data);
            admin.popupCenter({
                title: title,
                area: '550px',
                path: basePath + 'batch/task/editForm',
                finish: function () {
                    table.reload('task-table', {});
                }
            });
        };

        const del = function (id) {
            layer.confirm('确定要删除吗？', function () {
                layer.load(2);
                $.post(basePath + 'batch/task/delete', {
                    taskId: id
                }, function (data) {
                    layer.closeAll('loading');
                    if (data.success) {
                        layer.msg(data.msg, {icon: 1});
                        table.reload('task-table', {});
                    } else {
                        layer.msg(data.msg, {icon: 2});
                    }
                });
            });
        };

        // 搜索按钮点击事件
        $('#task-btn-search').click(function () {
            const value = $('#task-search-value').val();
            table.reload('task-table', {where: {searchValue: value}});
        });

    });
</script>